import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo2 {

    private static int count = 0;

    private static Lock lock = new ReentrantLock();

    private static void inc() {
        lock.lock();
        count ++;
        // 重入
        decr();
        lock.unlock();
    }

    private static void decr() {
        lock.lock();
        count --;
        lock.unlock();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Thread(ReentrantLockDemo2::inc).start();
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }

}
